<?php

namespace app\api\controller\v1;

use addons\litestore\model\Litestoregoodsspec;
use addons\litestore\model\Wxlitestoregoods;
use app\admin\model\litestore\Litestoregoods;
use app\admin\model\litestore\Litestorenews;
use app\admin\model\Profitorder;
use app\admin\model\Profitorderundo;
use app\admin\model\Profitvip;
use app\admin\model\Publishwork;
use app\admin\model\Shopjob;
use app\admin\model\Shopjobuser;
use app\admin\model\Shopuser;
use app\admin\model\Workaddress;
use app\admin\model\Worktype;
use app\api\service\ProfitService;
use app\common\controller\Api;
use think\Db;
use think\Exception;

/**
 */
class Shop extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function search(){
        $keyword = $this->request->param('keyword');
        $page = $this->request->param('page');

        $limit = 10;
        $offset = ($page - 1) * $limit;

        $where = array();
        if($keyword != ''){
            $where['merchantName']=array('like','%'.$keyword.'%');
        }
        $shopModel = new \app\admin\model\Shop();
        $list = $shopModel->where($where)->limit($offset, $limit)->select();
        $this->success('',$list);

    }

    public function selectShop(){
        $user = $this->auth->getUser();
        $uid = $user['id'];
        $id = $this->request->param('id');

        $shopUserModel = new Shopuser();
        $shopuser= $shopUserModel->where('uid',$uid)->where('status',1)->find();
        if($shopuser){
            $shopUserModel->where('id',$shopuser['id'])->update(['status'=>0]);
        }else{
            $shopUserModel->insert(['uid'=>$uid,'shopId'=>$id,'createtime'=>time(),'status'=>-1]);
        }
        $this->success('');
    }

    public function join(){

        $user = $this->auth->getUser();
        $uid = $user['id'];
        $id = $this->request->param('id');
        $shopJobModel = new Shopjob();
        $shopJob = $shopJobModel->where('id',$id)->find();
        $shopid = $shopJob['companyid'];

        $shopUserJobModel = new Shopjobuser();
        $shopUserJob = $shopUserJobModel->where('jobid',$id)->where('uid',$uid)->find();
        if($shopUserJob){

            $this->error('您已投递，请勿重复投递');
        }else{
            $data = array();
            $data['uid'] = $uid;
            $data['jobid'] = $id;
            $data['workid'] = $shopJob['workid'];
            $data['shopid'] = $shopid;
            $data['createtime'] = time();
            $shopUserJobModel->insert($data);
            $this->success('成功');
        }
    }

    public function job(){



        $id = $this->request->param('id');
        $shopJobModel = new Shopjob();
        $shopJob = $shopJobModel->where('id',$id)->find();
        $shopJob['tag'] = explode(",",$shopJob['tag']);
        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id',$shopJob['companyid'])->find();
        $imgs = explode(",",$shop['imgs']);
        $im = array();
        foreach ($imgs as $k=>$v){
           $d =  cdnurl($v,true);
           $im[] = $d;
        }
        $shop['img'] = $im;
        $shopJob['shop'] = $shop;

        $worktypeModel = new Worktype();
        $work = $worktypeModel->where('id',$shopJob['workid'])->find();
        $shopJob['work'] = $work;
        $this->success('',$shopJob);
    }

    public function addUser(){
        $user = $this->auth->getUser();
        $shopId= $this->request->param('shopId');
        $shopUserModel = new Shopuser();
        $shopUser = $shopUserModel->where('shopId',$shopId)->where('uid',$user['id'])->find();
        if($shopUser && $shopUser['status'] == -1){
            $this->error('正在审核中');
        }

        $data = array();
        $data['uid']= $user['id'];
        $data['shopId']=$shopId;
        $data['createtime']=time();

        $shopUserModel->insert($data);
        $this->success('成功');

    }


    /**
     * 店铺的工作列表
     */
    public function workList(){

        $this->success('成功','');

    }

}
